package org.study.interceptors;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

@Intercepts({
        @Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class, Integer.class})
})
public class MyInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler hanlder= (StatementHandler) invocation.getTarget();
        String sql = hanlder.getBoundSql().getSql();
        MetaObject metaObject = SystemMetaObject.forObject(hanlder);
        Object value = metaObject.getValue("parameterHandler.parameterObject");
        if(value!=null){
            metaObject.setValue("parameterHandler.parameterObject",2);
        }
        hanlder.getParameterHandler().getParameterObject();
        System.out.println(sql);
        return invocation.proceed();
    }

}
